package com.hello.fastboot.test.controller;

import com.gitee.huanminabc.fastboot.annotation.Value;
import com.gitee.huanminabc.fastboot.annotation.container.RestController;
import com.gitee.huanminabc.fastboot.event.AppEventPublisher;
import com.gitee.huanminabc.fastboot.web.annotation.http.GetMapping;
import com.gitee.huanminabc.fastboot.web.annotation.http.PostMapping;
import com.gitee.huanminabc.fastboot.web.annotation.http.RequestMapping;
import com.gitee.huanminabc.fastboot.web.annotation.http.RequestParam;
import com.gitee.huanminabc.jlambda_sql.warapper.LambdaQueryWrapper;
import com.gitee.huanminabc.jlambda_sql.warapper.LambdaWrapper;
import com.gitee.huanminabc.jnetty.base.ByteBufUtil;
import com.gitee.huanminabc.nullchain.common.NullResult;
import com.hello.fastboot.test.dao.UserManager;
import com.hello.fastboot.test.entity.UserEntity;
import com.hello.fastboot.test.entity.WorkEntity;
import com.hello.fastboot.test.event.TestEvent;
import com.hello.fastboot.test.service.WorkService;
import io.netty.buffer.ByteBuf;
import io.netty.handler.codec.http.multipart.FileUpload;
import lombok.Data;

import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * 简要描述
 *
 * @Author: huanmin
 * @Date: 2025/4/1 00:09
 * @Version: 1.0
 * @Description: 文件作用详细描述....
 */
@RestController
@Data
@RequestMapping("/work")
public class WorkController {

    @Value("${application.name}")
    private String projectName;
    private final WorkService workService;
    private final UserManager userManage;

    @GetMapping("/getWork")
    public NullResult<WorkEntity> getWork(
//            @Validated WorkEntity work123,
            WorkEntity work123,
            String workId,
            @RequestParam String workName,
            @RequestParam(value = "date",required = false) Date date

    ) {

        System.out.println(projectName);
        System.out.println(work123);
        AppEventPublisher.publishEvent(new TestEvent("test"));
        WorkEntity work = workService.getWork(workId);
        LambdaQueryWrapper<UserEntity> sql =userManage.createQueryWrapper().LIMIT(1);
        List<UserEntity> userEntities = userManage.selectList(sql);
        System.out.println(userEntities);

        return NullResult.success(work);
    }

    @PostMapping("/getJSON")
    public NullResult<WorkEntity> getJSON(WorkEntity workEntity) throws IOException {
        System.out.println(workEntity);
        return NullResult.success(workEntity);
    }

    @PostMapping("/file")
    public NullResult<String> getFile(FileUpload fileUpload) throws IOException {
        ByteBuf byteBuf = fileUpload.getByteBuf();
        String str = ByteBufUtil.byteBufToStr(byteBuf);
        return NullResult.success(str);
    }
    @PostMapping("/files")
    public NullResult<String> getFiles(FileUpload[] fileUpload,@RequestParam("name") String name) throws IOException {
        System.out.println(name);
        for (FileUpload upload : fileUpload) {
            ByteBuf byteBuf = upload.getByteBuf();
            String str = ByteBufUtil.byteBufToStr(byteBuf);
            System.out.println(str);
        }

        return NullResult.success("str");
    }

    @PostMapping("/fromUrlencoded")
    public NullResult<WorkEntity> fromUrlencoded(        WorkEntity work123,
                                                         @RequestParam("workId") String workId,
                                                         @RequestParam("workName") String workName,
                                                         @RequestParam("date") Date date) throws IOException {
        System.out.println(work123);
        WorkEntity work = workService.getWork(workId);
        return NullResult.success(work);
    }

    //发布事件
    @PostMapping("/publishEvent")
    public NullResult<String> publishEvent(@RequestParam("message") String message) {
        AppEventPublisher.publishEvent(new TestEvent(message));
        return NullResult.success("Event published: " + message);
    }
}
